package ph.greenova.service.impl;

import ph.greenova.persistence.model.Person;
import ph.greenova.persistence.model.Project;
import ph.greenova.service.serviceinterface.ProjectService;
import ph.greenova.web.dto.PersonDTO;
import ph.greenova.web.dto.ProjectDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By: adrian
 * Date Created: 5:12 PM 6/5/13
 */
//@Service
public class ProjectServiceImpl extends BaseService implements ProjectService {


    @Override
    public List<ProjectDTO> getAllProjects() {

        return transformEntityListToDTOListForProject(getProjectDAO().findAll());
    }

    @Override
    public void createNewProject(final ProjectDTO dto) {
        final Project newProject = new Project();

        newProject.setBudget(dto.getBudget());
        newProject.setRemarks(dto.getRemarks());
        newProject.setCode(dto.getCode());
        newProject.setName(dto.getName());

        getProjectDAO().create(newProject);
    }

    @Override
    public List<PersonDTO> getAllProjectMembers(final Long projectId) {
        return transformEntityListToDTOListForPersons(getProjectDAO().getAllProjectMembers(projectId));
    }

    @Override
    public List<PersonDTO> getAllNotMembers(final Long projectId) {
        List<Person> members = getProjectDAO().getAllProjectMembers(projectId);
        List<Person> allPersons = getPersonDAO().findAll();
        List<Person> nonMembers = new ArrayList<Person>();

        Boolean isMember;
        if (members != null) {
            for (Person p : allPersons) {
                isMember = false;
                for (Person p2 : members) {
                    if (p.getId().equals(p2.getId())) {
                        isMember = true;
                    }
                }

                if (!isMember) {
                    nonMembers.add(p);
                }
            }
        }
        return transformEntityListToDTOListForPersons(nonMembers);
    }

    @Override
    public void assignPersonToProject(final Long projectId, final Long personId) {
        getProjectDAO().assignPersonToProject(projectId, getPersonDAO().find(personId));
    }

    @Override
    public void unassignPersonFromProject(final Long projectId, final Long personId) {
        getProjectDAO().unAssignPersonFromProject(projectId, personId);
    }

    private List<PersonDTO> transformEntityListToDTOListForPersons(List<Person> entities) {
        List<PersonDTO> dtos = new ArrayList<PersonDTO>();
        PersonDTO dto;
        if (entities != null) {
            for (Person p : entities) {
                dto = new PersonDTO();
                dto.setId(String.valueOf(p.getId()));
                dto.setFirstName(p.getFirstName());
                dto.setLastName(p.getLastName());
                dtos.add(dto);
            }
        }
        return dtos;

    }

    private List<ProjectDTO> transformEntityListToDTOListForProject(List<Project> entities) {
        List<ProjectDTO> dtos = new ArrayList<ProjectDTO>();
        ProjectDTO dto;
        if (entities != null) {
            for (Project p : entities) {
                dto = new ProjectDTO();
                dto.setId(String.valueOf(p.getId()));
                dto.setBudget(p.getBudget());
                dto.setCode(p.getCode());
                dto.setName(p.getName());
                dtos.add(dto);
            }
        }
        return dtos;

    }
}
